package com.lianniu.redis;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.Pipeline;


/**
 * @ClassName：RedisUtils
 * @Description:redis utils包
 * @Author:wgn
 * @Since: 2016年1月12日12:38:05
 * @Version:1.1
 */
public class RedisUtils {
	private static final Logger log = Logger.getLogger(RedisUtils.class);

	@Autowired
	private JedisSentinelPool jedisPool;

	private static RedisUtils redisUtils;

	@PostConstruct
	public void init() {
		log.info("Redis  连接中................");
		redisUtils = this;
		redisUtils.jedisPool = jedisPool;
		if (jedisPool == null) {
			log.info("Redis  连接失败.................. ");
		} else {
			log.info("Redis  连接成功.................. ");
		}
	}

	/**
	 * 
	 * @Description：String--设置指定 key的值
	 * @Param:key value
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日12:43:17
	 */
	public static  String set(String key, String value) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.set(key, value);
		} catch (Exception e) {
			log.error("RedisUtils set param:" + key + "+" + value);
			log.error("RedisUtils set defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);	
		}
		return result;
	}
	
	
	/**
	 * 
	 * @Description：String--设置指定 key的值
	 * @Param:key value
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日12:43:17
	 */
	public static  String setTimeOut(String key, String value,int timeout) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis =RedisUtils.getJedis();
			result = jedis.set(key, value);
			jedis.expire(key, timeout);
		} catch (Exception e) {
			log.error("RedisUtils set param:" + key + "+" + value);
			log.error("RedisUtils set defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);	
		}
		return result;
	}
	
	/**
	 * 
	 * @Description：String--设置指定 key的过期时间
	 * @Param:key value
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日12:43:17
	 */
	public static  String expire(String key, int timeout) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			jedis.expire(key, timeout);
		} catch (Exception e) {
			log.error("RedisUtils set param:" + key + "+" +timeout);
			log.error("RedisUtils set defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);	
		}
		return result;
	}

	/**
	 * @Description：String -- 获取指定 key 的值
	 * @Param:key
	 * @Return:String
	 * @Author:wgn
	 * @Date:2016年1月12日12:45:04
	 */
	public static  String get(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("RedisUtils get param:" + key);
			log.error("RedisUtils get defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：string -- 判断指定 key是否有满足的元素
	 * @Param:key
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日12:46:21
	 */
	public  static Boolean exists(String key) {
		Jedis jedis = null;
		Boolean result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.exists(key);
		} catch (Exception e) {
			log.error("RedisUtils exists param:" + key);
			log.error("RedisUtils exists defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：String -- key 存在是删除 key
	 * @Param:key
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日12:49:03
	 */

	public static Long del(String key) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.del(key);
		} catch (Exception e) {
			log.error("RedisUtils del param:" + key);
			log.error("RedisUtils del defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：String -- 指定元素字符串拼接
	 * @Param:key str
	 * @Return:Long
	 * @Author:wgn
	 * @Date:2016年1月13日10:15:18
	 */
	public static Long append(String key, String str) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.append(key, str);
		} catch (Exception e) {
			log.error("RedisUtils append param:" + key + "+" + str);
			log.error("RedisUtils append defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：hash -- 将哈希表 key 中的字段 field 的值设为 value
	 * @Param:key field value
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日12:53:38
	 */
	public static Long hashSet(String key, String field, String value) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.hset(key, field, value);
		} catch (Exception e) {
			log.error("RedisUtils hset param:" + key + "+" + field + "+"
					+ value);
			log.error("RedisUtils hset defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：hash -- 获取存储在哈希表中key 指定字段 field的值
	 * @Param:String key String field
	 * @Return:满足条件的元素
	 * @Author:wgn
	 * @Date:2016年1月12日12:55:34
	 */
	public static String hashGet(String key, String field) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.hget(key, field);
		} catch (Exception e) {
			log.error("RedisUtils hget param:" + key + "+" + field);
			log.error("RedisUtils hget defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：hsah 同时将多个 field-value (域-值)对设置到哈希表 key 中
	 * @Param:String key,Map<String,String> hash
	 * @Return:是否成功？
	 * @Author:wgn
	 * @Date:2016年1月12日12:59:34
	 */
	public static String hsahMset(String key, Map<String, String> hash) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.hmset(key, hash);
		} catch (Exception e) {
			log.error("RedisUtils hmset param:" + key + "+" + hash);
			log.error("RedisUtils hmset defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：hash --获取 key 所有给定字段 field 的值
	 * @Param:String key, String... fields
	 * @Return:满足条件的集合
	 * @Author:wgn
	 * @Date:2016年1月13日10:27:30
	 */
	public static List<String> hashMget(String key, String... fields) {
		Jedis jedis = null;
		List<String> list = null;
		try {
			jedis = RedisUtils.getJedis();
			list = jedis.hmget(key, fields);
		} catch (Exception e) {
			log.error("RedisUtils hmset param:" + key + "+" + fields);
			log.error("RedisUtils hmset defeat:" + e.getMessage());
			return list;
		} finally {
			RedisUtils.close(jedis);
		}
		return list;
	}

	/**
	 * @Description：hash -- 删除一个或多个哈希表 key 字段 field 的值
	 * @Param:String key,String...fields
	 * @Return:删除是否成功
	 * @Author:wgn
	 * @Date:2016年1月12日13:38:02
	 */
	public static Long hashDel(String key, String... fields) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.hdel(key, fields);
		} catch (Exception e) {
			log.error("RedisUtils hmset param:" + key + "+" + fields);
			log.error("RedisUtils hmset defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：hash -- 查看哈希表 key 中，指定的字段是否存在。
	 * @Param:String key String field
	 * @Return:是否存在？
	 * @Author:wgn
	 * @Date:2016年1月12日13:41:31
	 */
	public static Boolean hashExists(String key, String field) {
		Jedis jedis = null;
		Boolean result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.hexists(key, field);
		} catch (Exception e) {
			log.error("RedisUtils hexists param:" + key + "+" + field);
			log.error("RedisUtils hexists defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：hash -- 获取哈希表 key 中字段的数量
	 * @Param:String key
	 * @Return:集合的数量
	 * @Author:wgn
	 * @Date:2016年1月12日13:45:52
	 */
	public static Long hashLen(String key) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.hlen(key);
		} catch (Exception e) {
			log.error("RedisUtils hlen param:" + key);
			log.error("RedisUtils hlen defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：hash -- 获取所有哈希表中的字段
	 * @Param:String key
	 * @Return:满足条件的set<String>集合
	 * @Author:wgn
	 * @Date:2016年1月12日13:50:01
	 */
	public static Set<String> hashKeys(String key) {
		Jedis jedis = null;
		Set<String> set = null;
		try {
			jedis = RedisUtils.getJedis();
			set = jedis.hkeys(key);
		} catch (Exception e) {
			log.error("RedisUtils hkeys param:" + key);
			log.error("RedisUtils hkeys defeat:" + e.getMessage());
			return set;
		} finally {
			RedisUtils.close(jedis);
		}
		return set;
	}

	/**
	 * @Description：hash -- 获取哈希表key中所有值
	 * @Param:String key
	 * @Return:满足条件的List<String>集合
	 * @Author:wgn
	 * @Date:2016年1月12日13:54:56
	 */
	public static List<String> hashVals(String key) {
		Jedis jedis = null;
		List<String> list = null;
		try {
			jedis = RedisUtils.getJedis();
			list = jedis.hvals(key);
		} catch (Exception e) {
			log.error("RedisUtils hvals param:" + key);
			log.error("RedisUtils hvals defeat:" + e.getMessage());
			return list;
		} finally {
			RedisUtils.close(jedis);
		}
		return list;
	}

	/**
	 * @Description：hash -- 获取在哈希表中指定 key 的所有字段和值
	 * @Param:String key
	 * @Return:满足条件的Map<String,String>集合
	 * @Author:wgn
	 * @Date:2016年1月12日13:58:11
	 */
	public static Map<String, String> hashGetAll(String key) {
		Jedis jedis = null;
		Map<String, String> map = null;
		try {
			jedis = RedisUtils.getJedis();
			map = jedis.hgetAll(key);
		} catch (Exception e) {
			log.error("RedisUtils hgetAll param:" + key);
			log.error("RedisUtils hgetAll defeat:" + e.getMessage());
			return map;
		} finally {
			RedisUtils.close(jedis);
		}
		return map;

	}

	/**
	 * @Description：List -- 在列表中添加一个或多个值
	 * @Param:String key , String...strings
	 * @Return:long
	 * @Author:wgn
	 * @Date:2016年1月12日14:03:25
	 */
	public static Long listRpush(String key, String... strings) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.rpush(key, strings);
		} catch (Exception e) {
			log.error("RedisUtils rpush param:" + key);
			log.error("RedisUtils rpush defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：List -- 将一个或多个值插入到列表头部
	 * @Param:String key,String...strings
	 * @Return:long
	 * @Author:wgn
	 * @Date:2016年1月12日14:05:57
	 */
	public static Long listLpush(String key, String... strings) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.rpush(key, strings);
		} catch (Exception e) {
			log.error("RedisUtils lpush param:" + key);
			log.error("RedisUtils lpush defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：移除并获取列表最后一个元素
	 * @Param:key
	 * @Return:string
	 * @Author:wgn
	 * @Date:2016年1月13日10:57:01
	 */
	public static String listRpop(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.rpop(key);
		} catch (Exception e) {
			log.error("RedisUtils rpop param:" + key);
			log.error("RedisUtils rpop defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：移出并获取列表的第一个元素
	 * @Param:string key
	 * @Return:string 当前元素
	 * @Author:wgn
	 * @Date:2016年1月13日10:58:15
	 */
	public static String listLpop(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.lpop(key);
		} catch (Exception e) {
			log.error("RedisUtils lpop param:" + key);
			log.error("RedisUtils lpop defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：通过索引获取列表中的元素
	 * @Param:String key, long index
	 * @Return:String
	 * @Author:wgn
	 * @Date:2016年1月13日11:05:46
	 */
	public static String listLindex(String key, long index) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.lindex(key, index);
		} catch (Exception e) {
			log.error("RedisUtils lindex param:" + key + "+" + index);
			log.error("RedisUtils lindex defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：List -- 获取列表指定范围内的元素
	 * @Param:String key,long start,long end
	 * @Return:List<String>
	 * @Author:wgn
	 * @Date:2016年1月12日14:07:57
	 */
	public static List<String> listLrange(String key, long start, long end) {
		Jedis jedis = null;
		List<String> list = null;
		try {
			jedis = RedisUtils.getJedis();
			list = jedis.lrange(key, start, end);
		} catch (Exception e) {
			log.error("RedisUtils lrange param:" + key + "+" + start + "+"
					+ end);
			log.error("RedisUtils lrange defeat:" + e.getMessage());
			return list;
		} finally {
			RedisUtils.close(jedis);
		}
		return list;
	}

	/**
	 * @Description：List -- 对一个列表进行修剪(trim)，就是说，让列表只保留指定区间内的元素，不在指定区间之内的元素都将被删除。
	 * @Param:String key,long start,long end
	 * @Return:String
	 * @Author:wgn
	 * @Date:2016年1月12日14:07:57
	 */
	public static String listLtrim(String key, long start, long end) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.ltrim(key, start, end);
		} catch (Exception e) {
			log.error("RedisUtils ltrim param:" + key + "+" + start + "+" + end);
			log.error("RedisUtils ltrim defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;

	}

	/**
	 * @Description：set -- 向集合添加一个或多个成员
	 * @Param:String key String...members
	 * @Return:是否添加成功
	 * @Author:wgn
	 * @Date:2016年1月12日14:14:46
	 */
	public static Long setSadd(String key, String... members) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis =RedisUtils.getJedis();
			result = jedis.sadd(key, members);
		} catch (Exception e) {
			log.error("RedisUtils sadd param:" + key + "+" + members);
			log.error("RedisUtils sadd defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：移除集合中一个或多个成员
	 * @Param:String key String... members
	 * @Return:是否成功
	 * @Author:wgn
	 * @Date:2016年1月13日11:12:08
	 */
	public static Long setSrem(String key, String... members) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.srem(key, members);
		} catch (Exception e) {
			log.error("RedisUtils srem param:" + key + "+" + members);
			log.error("RedisUtils srem defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：获取集合的成员数
	 * @Param:key
	 * @Return:类型-long 成员数量
	 * @Author:wgn
	 * @Date:2016年1月12日10:32:03
	 */
	public static Long setScard(String key) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.scard(key);
		} catch (Exception e) {
			log.error("RedisUtils scard param:" + key);
			log.error("RedisUtils scard defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * 
	 * @Description： set： 判断 member 元素是否是集合 key 的成员
	 * @Param:key value
	 * @Return:boolen
	 * @Author:wgn
	 * @Date:2016年1月12日10:18:17
	 */
	public static Boolean setSismember(String key, String member) {
		Jedis jedis = null;
		Boolean result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.sismember(key, member);
		} catch (Exception e) {
			log.error("RedisUtils sismember param:" + key + "+" + member);
			log.error("RedisUtils sismember defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：set 返回集合中一个或多个随机数
	 * @Param:key count随机数
	 * @Return:类型- list<String>
	 * @Author:wgn
	 * @Date:2016年1月12日10:27:31
	 */
	public static List<String> setSrandmember(String key, int count) {
		Jedis jedis = null;
		List<String> list = null;
		try {
			jedis = RedisUtils.getJedis();
			list = jedis.srandmember(key, count);
		} catch (Exception e) {
			log.error("RedisUtils srandmember param:" + key);
			log.error("RedisUtils srandmember defeat:" + e.getMessage());
			return list;
		} finally {
			RedisUtils.close(jedis);
		}
		return list;
	}

	/**
	 * @Description：set-移除并返回集合中的一个随机元素
	 * @Param:key
	 * @Return:string
	 * @Author:wgn
	 * @Date:2016年1月12日10:36:20
	 */
	public static String setSpop(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.spop(key);
		} catch (Exception e) {
			log.error("RedisUtils spop param:" + key);
			log.error("RedisUtils spop defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：set -移除并返回集合中的多个随机元素
	 * @Param:key count
	 * @Return:Set<String>
	 * @Author:wgn
	 * @Date:2016年1月12日10:40:32
	 */
	public static Set<String> setSpop(String key, long count) {
		Jedis jedis = null;
		Set<String> set = null;
		try {
			jedis = RedisUtils.getJedis();
			set = jedis.spop(key, count);
		} catch (Exception e) {
			log.error("RedisUtils spop param:" + key);
			log.error("RedisUtils spop defeat:" + e.getMessage());
			return set;
		} finally {
			RedisUtils.close(jedis);
		}
		return set;
	}

	/**
	 * @Description：sorted set -- 向有序集合添加一个或多个成员
	 * @Param:key Map<String,Double>
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日10:54:58
	 */
	public static Long zsetZadd(String key, Map<String, Double> scoreMembers) {

		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zadd(key, scoreMembers);
		} catch (Exception e) {
			log.error("RedisUtils zadd param:" + key + "+" + scoreMembers);
			log.error("RedisUtils zadd defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：sorted set -- 向有序集合 更新已存在成员的分数
	 * @Param:key score member
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日11:01:52
	 */
	public static Long zsetZadd(String key, Double score, String member) {

		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zadd(key, score, member);
		} catch (Exception e) {
			log.error("RedisUtils zadd param:" + key + "+" + score + "+"
					+ member);
			log.error("RedisUtils zadd defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：sorted set 移除有序集合中的一个或多个成员
	 * @Param:key members[]
	 * @Return:boolean
	 * @Author:wgn
	 * @Date:2016年1月12日11:03:07
	 */
	public static Long zsetZrem(String key, String... members) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zrem(key, members);
		} catch (Exception e) {
			log.error("RedisUtils zrem param:" + key + "+" + members);
			log.error("RedisUtils zrem defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：sorted set-- 获取有序集合的成员数
	 * @Param:key
	 * @Return:long
	 * @Author:wgn
	 * @Date:2016年1月12日11:10:09
	 */
	public static Long zsetZcard(String key) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zcard(key);
		} catch (Exception e) {
			log.error("RedisUtils zcard param:" + key);
			log.error("RedisUtils zcard defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：有序集合中对指定成员的分数加上增量 increment
	 * @Param:String key,Double score,String member
	 * @Return:Double
	 * @Author:wgn
	 * @Date:2016年1月12日15:09:55
	 */
	public static Double zsetZincrby(String key, Double score, String member) {
		Jedis jedis = null;
		Double result = 0.0;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zincrby(key, score, member);
		} catch (Exception e) {
			log.error("RedisUtils zincrby param:" + key + "+" + score + "+"
					+ member);
			log.error("RedisUtils zincrby defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：计算在有序集合中指定区间分数的成员数
	 * @Param:String key,String min,String max
	 * @Return:long
	 * @Author:wgn
	 * @Date:2016年1月12日15:21:04
	 */
	public static Long zsetZcount(String key, String min, String max) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zcount(key, min, max);
		} catch (Exception e) {
			log.error("RedisUtils zcount param:" + key + "+" + min + "+" + max);
			log.error("RedisUtils zcount defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;

	}

	/**
	 * @Description：返回有序集合中指定成员的索引
	 * @Param:String key ,String member
	 * @Return:long
	 * @Author:wgn
	 * @Date:2016年1月12日15:31:02
	 */
	public static Long zsetZrank(String key, String member) {
		Jedis jedis = null;
		Long result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zrank(key, member);
		} catch (Exception e) {
			log.error("RedisUtils zrank param:" + key + "+" + member);
			log.error("RedisUtils zrank defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：返回有序集中，成员的分数值
	 * @Param:String key ,String member
	 * @Return:double
	 * @Author:wgn
	 * @Date:2016年1月12日15:38:01
	 */
	public static Double zsetZscore(String key, String member) {
		Jedis jedis = null;
		Double result = null;
		try {
			jedis = RedisUtils.getJedis();
			result = jedis.zscore(key, member);
		} catch (Exception e) {
			log.error("RedisUtils zscore param:" + key + "+" + member);
			log.error("RedisUtils zscore defeat:" + e.getMessage());
			return result;
		} finally {
			RedisUtils.close(jedis);
		}
		return result;
	}

	/**
	 * @Description：通过索引区间返回有序集合成指定区间内的成员
	 * @Param:String key, long start, long end
	 * @Return:Set<String> 集合元素
	 * @Author:wgn
	 * @Date:2016年1月12日15:45:39
	 */
	public static Set<String> zsetZrange(String key, long start, long end) {
		Jedis jedis = null;
		Set<String> set = null;
		try {
			jedis = RedisUtils.getJedis();
			set = jedis.zrange(key, start, end);
		} catch (Exception e) {
			log.error("RedisUtils zrange param:" + key + "+" + start + "+"
					+ end);
			log.error("RedisUtils zrange defeat:" + e.getMessage());
			return set;
		} finally {
			RedisUtils.close(jedis);
		}
		return set;
	}

	public static String lockOpen(Jedis jedis, String lockname, int acquirt_time,
			int lock_time) {

		String code = UUID.randomUUID().toString();
		lockname = "lock" + lockname;
		long time = new Date().getTime();
		long end = time + acquirt_time;
		while (new Date().getTime() < end) {
			if (jedis.setnx(lockname, code) == 1) {
				jedis.expire(lockname, lock_time);
				return code;
			} else if (jedis.ttl(lockname) == 1) {
				jedis.expire(lockname, lock_time);
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "";
	}

	public static boolean lockClose(Jedis jedis, String lockname, String code) {
		Pipeline pipe = jedis.pipelined();
		lockname = "lock" + lockname;
		while (true) {
			try {
				pipe.watch(lockname);
				if (pipe.get(lockname).equals(code)) {
					pipe.multi();
					pipe.del(lockname);
					pipe.exec();
					return true;

				}
				break;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		return false;

	}

	/**
	 * 从连接池中获取jedis连接
	 */
	public static Jedis getJedis() {
		Jedis jedis = redisUtils.jedisPool.getResource();
		jedis.auth("123qwe");
		return jedis;
	}

	/**
	 * 释放以损坏的jedis.
	 */
    public static void closeBroken(Jedis jedis) {
		if (jedis != null) {
			try {
				redisUtils.jedisPool.returnBrokenResource(jedis);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 从连接池中释放jedis
	 */
	public static void close(Jedis jedis) {
		if (jedis != null) {
			try {
				jedis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
